/*
**
***	大图切换，基本效果已完成。
***	图片导航细节为完善。
*** 5.12 
**
*/
$(function(){
	var k = 0;
	var ImgW = [], ImgH = [];
	var _length = $(".ShowImgNav").find("li").length;
	$(".ShowImgNav").width($(".ShowImgNav").find("li").outerWidth(true)*_length);	/*图片导航宽度*/
	$(".ImgNums").text(_length);/*前台图片个数*/
	$(".ImgNowNums").text(k+1);
	$(".ShowImgNav").find("li:first-child").addClass("NowImg");
	ImgSrc($(".ShowImg").find("img"),$(".ShowImgNav").find("img:first-child").attr("src"));
	ImgTxt($(".ShowImgInfo").children(".ImgTxt:eq(0)"));
	PrimaryImg($(".ShowImgNav").find("img:first-child"));	/*初始展示第一个*/
	
	$(".ShowImgNav").find("li").each(function(i, o) {
        $(o).click(function(){
			AddClass($(this));
			ImgSrc($(".ShowImg").find("img"),$(this).find("img").attr("src"));
			PrimaryImg($(this).find("img"));
			k = i;
			$(".ImgNowNums").text(k+1);
			ImgTxt($(".ShowImgInfo").children(".ImgTxt").eq(k));
		});
    });
	$(".RarrowBtn").click(function(){	/*大图右切换*/
		if(k>=_length-1) k = _length-1;
		else k++;
		AddClass($(".ShowImgNav").find("li").eq(k));
		ImgSrc($(".ShowImg").find("img"),$(".ShowImgNav").find("img").eq(k).attr("src"));	/*切换图片*/
		PrimaryImg($(".ShowImgNav").find("img").eq(k));
		$(".ImgNowNums").text(k+1);
		ImgTxt($(".ShowImgInfo").children(".ImgTxt").eq(k));
	});
	
	$(".LarrowBtn").click(function(){	/*大图左切换*/
		if(k<=0) k = 0;
		else k--;
		AddClass($(".ShowImgNav").find("li").eq(k));
		ImgSrc($(".ShowImg").find("img"),$(".ShowImgNav").find("img").eq(k).attr("src"));	/*切换图片*/
		PrimaryImg($(".ShowImgNav").find("img").eq(k));
		$(".ImgNowNums").text(k+1);
		ImgTxt($(".ShowImgInfo").children(".ImgTxt").eq(k));
	});
	
	var a = Math.floor($(".ShowImgNav").width()/1026);
	var b = 0;
	$(".RarrowNavBtn").click(function(){	/*导航切换*/
		if(b>=a) b = b;
		else b++;
		if($(".ShowImgNav").width()>1026){
			$(".ShowImgNav").animate({marginLeft:-1026*b+"px"})
		}
	});
	$(".LarrowNavBtn").click(function(){	/*导航切换*/
		if(b<=0) b = 0;
		else b--;
		if($(".ShowImgNav").width()>1026){
			$(".ShowImgNav").animate({marginLeft:-1026*b+"px"})
		}
	});
	
	$(".ImgFDBtn").click(function(){	/*图片放大*/
		$(this).attr({"href":$(".ShowImg").find("img").attr("src"), "target": "_blank"})
		//window.open({"url":$(".ShowImg").find("img").attr("src")})
	});
	
	function ImgSrc(obj,src){	/*图片路径*/
		obj.attr("src",src);
	}
	
	function AddClass(obj){		/*添加图片导航样式*/
		obj.addClass("NowImg").siblings().removeClass("NowImg");
	}
	
	function ImgTxt(obj){		/*图片说明*/
		obj.show().siblings(".ImgTxt").hide();
	}
	
	function PrimaryImg(img){	/*获取图片原尺寸*/
		var pw = $(".ShowImg").width(), ph = $(".ShowImg").height();
		var pp = pw/ph;
		var iw, ih, ip;
		$("<img>").attr("src",$(img).attr("src")).load(function(){
			iw = this.width;
			ih = this.height;
			ip = iw/ih;
			if(iw>=pw&&ih>=ph){
				if(ip>pp){
					//ih = ph;
					//iw = ih*ip
					iw = pw;
					ih = iw/ip;
					$(".ShowImg").find("span").css({"marginLeft":(pw-iw)/2+"px", "marginTop":(ph-ih)/2+"px", "width": iw+"px", "height": ih+"px"});
					$(".ImgFDBtn").css({"right": (iw-pw)/2+10+"px", "top": 0});
					/*放大按钮跟随*/
				}else{
					//iw = pw;
					//ih = iw/ip;
					ih = ph;
					iw = ih*ip
					$(".ShowImg").find("span").css({"marginLeft":(ph-ih)/2+"px","marginTop":(ph-ih)/2+"px", "width": iw+"px", "height": ih+"px"});
					$(".ImgFDBtn").css({"right": "10px", "top": (ih-ph)/2+"px"});
					/*放大按钮跟随*/
				}
			}else if(iw>=pw&&ih<ph){
				$(".ShowImg").find("span").css({"marginLeft":(pw-iw)/2+"px", "marginTop":(ph-ih)/2+"px", "width": iw+"px", "height": ih+"px"});
				$(".ImgFDBtn").css({"right": (iw-pw)/2+10+"px", "top": 0});
				/*放大按钮跟随*/
			}else if(iw<pw&&ih>=ph){
				ih = ph;
				iw = ih*ip
				$(".ShowImg").find("span").css({"marginLeft":"0px", "marginTop":(ph-ih)/2+"px", "width": iw+"px", "height": ih+"px"});
				$(".ImgFDBtn").css({"right": "10px", "top": 0});
				/*放大按钮跟随*/
			}else{
				$(".ShowImg").find("span").css({"marginLeft":"0px", "marginTop":(ph-ih)/2+"px", "width": iw+"px", "height": ih+"px"});
				var _top = ih>ph ? (ih-ph)/2:0 ;
				$(".ImgFDBtn").css({"right": "10px", "top": _top+"px"});
				/*放大按钮跟随*/
			}
			
		})
if(navigator.userAgent.indexOf("MSIE 7.0")>0||navigator.userAgent.indexOf("MSIE 8.0")>0){
			var image = new Image();
			image.src = img.attr("src");
			iw = image.width;
			ih = image.height;
			ip = iw/ih;
			if(iw>=pw&&ih>=ph){
				if(ip>pp){
					ih = ph;
					iw = ih*ip
					$(".ShowImg").find("span").css({"marginLeft":(pw-iw)/2+"px", "marginTop":(ph-ih)/2+"px", "width": iw+"px", "height": ih+"px"});
					$(".ImgFDBtn").css({"right": (iw-pw)/2+10+"px", "top": 0});
					/*放大按钮跟随*/
				}else{
					iw = pw;
					ih = iw/ip;
					$(".ShowImg").find("span").css({"marginLeft":(ph-ih)/2+"px","marginTop":(ph-ih)/2+"px", "width": iw+"px", "height": ih+"px"});
					$(".ImgFDBtn").css({"right": (iw-pw)/2+10+"px", "top": (ih-ph)/2+"px"});
					/*放大按钮跟随*/
				}
			}else if(iw>=pw&&ih<ph){
				$(".ShowImg").find("span").css({"marginLeft":(pw-iw)/2+"px", "marginTop":(ph-ih)/2+"px", "width": iw+"px", "height": ih+"px"});
				$(".ImgFDBtn").css({"right": (iw-pw)/2+10+"px", "top": 0});
				/*放大按钮跟随*/
			}else{
				$(".ShowImg").find("span").css({"marginLeft":"0px", "marginTop":(ph-ih)/2+"px", "width": iw+"px", "height": ih+"px"});
				var _top = ih>ph ? (ih-ph)/2:0 ;
				$(".ImgFDBtn").css({"right": "10px", "top": _top+"px"});
				/*放大按钮跟随*/
			}
		} //ie 7 8未能获取图片尺寸 5.22已完善
	}
		
	
})